package com.paipaideli.ui;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.utils.preferrnces.FastData;
import com.paipaideli.ui.login.LoginActivity;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/*
 * 欢迎页
 * */
public class WelcomeActivity extends BasePresenterActivity {

    @Override
    public int getContentView() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {
        //安卓8.0权限的判断
        if (Build.VERSION.SDK_INT >= 26) {
            ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 1);
        }

        if (!FastData.getFirstInApp()) {
            //如果是第一次，做权限申请
            if (Build.VERSION.SDK_INT >= 23) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
                }
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                }
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
                }

            }
            timer(FastData.getFirstInApp());
        } else {
            //不是第一次，直接往下走
            timer(FastData.getFirstInApp());
        }
    }

    private void timer(boolean isFirst) {
        addDisposable(Observable.just("1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(r -> new CountDownTimer(1500, 500) {
                    public void onTick(long millisUntilFinished) {

                    }

                    public void onFinish() {
                        if (isFirst) {
                            if (FastData.isLogin()) {
                                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                                startActivity(intent);
                            } else {
//                                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                                startActivity(intent);
                            }
                        } else {
                            startActivity(new Intent(getApplicationContext(), GuidePagesActivity.class));
                        }
                        finish();
                    }
                }.start(), throwable -> {
                }));
    }

    /**
     * Activity屏蔽物理返回按钮
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
